package cn.ms.neural.common.log.mdc;

import java.util.UUID;

import org.apache.logging.log4j.ThreadContext;

/**
 * The Log4j2 Thread Context(MDC)
 * <br>
 * Including the ThreadContext when writing logs
 * The PatternLayout provides mechanisms to print the contents of the ThreadContext Map and Stack.
 * Use %X by itself to include the full contents of the Map.
 * Use %X{key} to include the specified key.
 * Use %x to include the full contents of the Stack.
 * 
 * @author lry
 */
public class Log4j2MDC<REQ, RES> {

	public RES biz_id(REQ req, IMDC<REQ, RES> mdc) throws Throwable {
		try {
			ThreadContext.put("serviceId", UUID.randomUUID().toString());
			ThreadContext.put("bizSeqNo", UUID.randomUUID().toString());
			ThreadContext.put("sysSeqNo", UUID.randomUUID().toString());
			return mdc.mdc(req);
		} finally {
			ThreadContext.clearAll();
		}
	}
	
}
